<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Executes an SQL statement, returning a result set as a PDOStatement object</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="pdo.prepare.html">PDO::prepare</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="pdo.quote.html">PDO::quote</a></div>
 <div class="up"><a href="class.pdo.html">PDO</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="pdo.query" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDO::query</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PECL pdo &gt;= 0.2.0)</p><p class="refpurpose"><span class="refname">PDO::query</span> &mdash; <span class="dc-title">
   Executes an SQL statement, returning a result set as a PDOStatement object
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-pdo.query-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type"><a href="class.pdostatement.html" class="type PDOStatement">PDOStatement</a></span> <span class="methodname"><strong>PDO::query</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$statement</code></span>
   )</div>


  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type"><a href="class.pdostatement.html" class="type PDOStatement">PDOStatement</a></span> <span class="methodname"><strong>PDO::query</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$statement</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$PDO::FETCH_COLUMN</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$colno</code></span>
   )</div>


  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type"><a href="class.pdostatement.html" class="type PDOStatement">PDOStatement</a></span> <span class="methodname"><strong>PDO::query</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$statement</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$PDO::FETCH_CLASS</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$classname</code></span>
   , <span class="methodparam"><span class="type">array</span> <code class="parameter">$ctorargs</code></span>
   )</div>


  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type"><a href="class.pdostatement.html" class="type PDOStatement">PDOStatement</a></span> <span class="methodname"><strong>PDO::query</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$statement</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$PDO::FETCH_INTO</code></span>
   , <span class="methodparam"><span class="type">object</span> <code class="parameter">$object</code></span>
   )</div>


  <p class="para rdfs-comment">
   <span class="function"><strong>PDO::query()</strong></span> executes an SQL statement in
   a single function call, returning the result set (if any) returned by the
   statement as a PDOStatement object.
  </p>
  <p class="para">
   For a query that you need to issue multiple times, you will realize better
   performance if you prepare a PDOStatement object using
   <span class="function"><a href="pdo.prepare.html" class="function">PDO::prepare()</a></span> and issue the statement with multiple
   calls to <span class="function"><a href="pdostatement.execute.html" class="function">PDOStatement::execute()</a></span>.
  </p>
  <p class="para">
   If you do not fetch all of the data in a result set before issuing your
   next call to <span class="function"><strong>PDO::query()</strong></span>, your call may fail. Call
   <span class="function"><a href="pdostatement.closecursor.html" class="function">PDOStatement::closeCursor()</a></span> to release the database
   resources associated with the PDOStatement object before issuing your
   next call to <span class="function"><strong>PDO::query()</strong></span>.
  </p>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Although this function is only documented as having a single
    parameter, you may pass additional arguments to this function.
    They will be treated as though you called
    <span class="function"><a href="pdostatement.setfetchmode.html" class="function">PDOStatement::setFetchMode()</a></span> on the resultant statement
    object.
   </p>
  </p></blockquote>

 </div>

 <div class="refsect1 parameters" id="refsect1-pdo.query-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">statement</code></em></dt>

      <dd>

       <p class="para">
        The SQL statement to prepare and execute.
       </p>
       <p class="para">
        Data inside the query should be <a href="pdo.quote.html" class="link">properly escaped</a>.
       </p>
      </dd>

     
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-pdo.query-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
    <span class="function"><strong>PDO::query()</strong></span> returns a PDOStatement object, or <strong><code>FALSE</code></strong>
    on failure.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-pdo.query-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-902">
    <p><strong>Example #1 Demonstrate PDO::query</strong></p>
    <div class="example-contents"><p>
     A nice feature of <span class="function"><strong>PDO::query()</strong></span> is that it
     enables you to iterate over the rowset returned by a
     successfully executed SELECT statement.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">getFruit</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$sql&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'SELECT&nbsp;name,&nbsp;color,&nbsp;calories&nbsp;FROM&nbsp;fruit&nbsp;ORDER&nbsp;BY&nbsp;name'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #0000BB">$sql</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\t"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'color'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\t"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'calories'</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
apple   red     150
banana  yellow  250
kiwi    brown   75
lemon   yellow  25
orange  orange  300
pear    green   150
watermelon      pink    90
</pre></div>
    </div>
   </div>
  </p>
 </div>



 <div class="refsect1 seealso" id="refsect1-pdo.query-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="pdo.exec.html" class="function" rel="rdfs-seeAlso">PDO::exec()</a> - 执行一条 SQL 语句，并返回受影响的行数</span></li>
    <li class="member"><span class="function"><a href="pdo.prepare.html" class="function" rel="rdfs-seeAlso">PDO::prepare()</a> - Prepares a statement for execution and returns a statement object</span></li>
    <li class="member"><span class="function"><a href="pdostatement.execute.html" class="function" rel="rdfs-seeAlso">PDOStatement::execute()</a> - 执行一条预处理语句</span></li>
   </ul>
  </p>
 </div>



</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="pdo.prepare.html">PDO::prepare</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="pdo.quote.html">PDO::quote</a></div>
 <div class="up"><a href="class.pdo.html">PDO</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
